package com.demo.spring.autowird;

import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

public class Autowired底层演示 {
	
	public static void main(String[] args) {
		// GenericApplicationContext是一个干净的容器
		GenericApplicationContext context = new GenericApplicationContext();
		context.registerBeanDefinition("bean1", new AnnotatedGenericBeanDefinition(Bean1.class));
		context.registerBeanDefinition("bean2", new AnnotatedGenericBeanDefinition(Bean2.class));
		context.registerBeanDefinition("bean3", new AnnotatedGenericBeanDefinition(Bean3.class));
		context.registerBeanDefinition("bean4", new AnnotatedGenericBeanDefinition(Bean4.class));
		
		// 用于解析String等类型的属性
		context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		// 用于解析@Autowired、@Value注解
		context.registerBeanDefinition("autowiredAnnotationBeanPostProcessor", new AnnotatedGenericBeanDefinition(AutowiredAnnotationBeanPostProcessor.class));
		// 用于解析@Resource、@PostConstuct注解
		context.registerBeanDefinition("commonAnnotationBeanPostProcessor", new AnnotatedGenericBeanDefinition(CommonAnnotationBeanPostProcessor.class));
		// 处理@ConfigurationProperties注解
		context.registerBeanDefinition("configurationPropertiesBindingPostProcessor", new AnnotatedGenericBeanDefinition(ConfigurationPropertiesBindingPostProcessor.class));
		
		context.refresh();
		
		System.out.println(context.getBean(Bean4.class));
	}

}
